<template>
    <section>

        <!--从右往左-->
        <el-radio-group v-model="direction">

        </el-radio-group>


        <!--添加框-->
        <el-dialog title1="添加" :visible.sync="dialogFormVisible1">
            <el-form :model="addForm1"  ref="addForm">
                <el-form-item label="明细名" :label-width="formLabelWidth">
                    <el-input v-model="addForm1.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm1('addForm1')">取 消</el-button>
                <el-button type="primary" @click="onSubmit1('addForm1')">确 定</el-button>
            </div>

        </el-dialog>

        <el-drawer title="数据字典明细" :visible.sync="drawer" :direction="direction" :before-close="handleClose" size="50%">
            <!--高级查询添加-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true">
                    <el-form-item>
                        <el-input v-model="dname1" placeholder="名字"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" v-on:click="getUsers1">查询</el-button>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleAdd1">新增</el-button>
                    </el-form-item>

                </el-form>
            </el-col>
            <!--列表展示-->
            <el-table :data="dictionaryDetailList" highlight-current-row v-loading="listLoading"
                      @selection-change="selsChange1"
                      style="width: 100%;">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column type="index" width="60"></el-table-column>
                <el-table-column prop="name" label="明细名"></el-table-column>

                <el-table-column label="操作">
                    <template scope="scope">
                        <el-button size="small" @click="handleEdit1(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel1(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--工具条-->
            <el-col :span="24" class="toolbar">

                <el-button type="danger" @click="batchRemove1" :disabled="this.sels1.length===0">批量删除</el-button>

                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange1" :page-size="pageSize1"
                               :total="total1" style="float:right;">
                </el-pagination>
            </el-col>

        </el-drawer>


        <!--添加框-->
        <el-dialog title="添加" :visible.sync="dialogFormVisible">

            <el-form :model="addForm"  ref="addForm">
                <el-form-item label="字典名" :label-width="formLabelWidth" >
                    <el-input v-model="addForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="字典编号" :label-width="formLabelWidth" >
                    <el-input v-model="addForm.sn" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('addForm')">取 消</el-button>
                <el-button type="primary" @click="onSubmit('addForm')">确 定</el-button>
            </div>
        </el-dialog>
        <!--高级查询添加-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item>
                    <el-input v-model="dname" placeholder="数据字典名"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>

            </el-form>
        </el-col>
        <!--列表展示-->
        <el-table :data="dictionaryTypeList" @row-dblclick="double1" highlight-current-row v-loading="listLoading"
                  @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column type="index" width="60"></el-table-column>
            <el-table-column prop="name" label="数据字典名"></el-table-column>
            <el-table-column prop="sn" label="编号"></el-table-column>

            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">

            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>

            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

    </section>
</template>

<script>
    export default {
        name: "Department",
        data() {
            return {
                drawer: false,
                direction: 'rtl',
                //高级查询字段
                dname: '',
                //明细查询
                dname1: '',
                //弹出框的标题
                title: '',
                //明细
                title1:'',
                //类型列表展示
                dictionaryTypeList: [],
                //明细
                dictionaryDetailList: [],
                //页面加载
                listLoading: '',
                currentPage: 1,
                pageSize: 2,
                total: 0,
                //明细
                currentPage1: 1,
                pageSize1: 3,
                total1: 0,
                //选中的数据
                sels: [],
                //明细选中
                sels1: [],
                parentId: '',
                //dialog显示
                dialogFormVisible: false,
                //明细弹出框
                dialogFormVisible1: false,
                addForm: {
                    id: '',
                    name: '',
                    sn: '',
                },
                //明细表单
                addForm1: {
                    id: '',
                    name: '',
                    types_id: '',
                },
                formLabelWidth: '120px'
            }
        },
        methods: {
            //重置。点击取消
            resetForm1() {
                this.dialogFormVisible1 = false
            },
            //明细提交
            onSubmit1(addForm1) {
                //提交校验,校验成功
                this.addForm1.types_id = this.parentId
                console.log(this.addForm1)
                this.$http.put("/dictDetail", this.addForm1).then(res => {
                    //关闭dialog
                    this.dialogFormVisible1 = false
                    //加载数据
                    this.loaddata1(this.parentId)
                });
            },
            //明细分页
            handleCurrentChange1(val) {
                // console.log(this.parentId)
                this.currentPage1 = val
                this.loaddata1(this.parentId)
            },
            //明细批量删除
            batchRemove1() {
                this.$confirm('此操作将永久删除这些数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/dictDetail/batchRemove", this.sels1).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loaddata1(this.parentId)
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //明细新增
            handleAdd1() {
                this.title1 = "添加";
                //清空表单
                for (let i in this.addForm1) {
                    this.addForm1[i] = ""
                }
                //显示修改框
                this.dialogFormVisible1 = true;
            },
            //明细查询
            getUsers1() {
                this.currentPage1 = 1
                this.loaddata1(this.parentId)
            },
            //明细删除
            handleDel1(index, row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    console.log(row)
                    this.$http.delete("/dictDetail/" + row.id).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loaddata1(this.parentId)
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //明细修改
            handleEdit1(index, row) {
                this.title1 = "修改";
                this.dialogFormVisible1 = true;
                //回显数据(一般数据)
                for (let x in this.addForm1) {
                    this.addForm1[x] = row[x]
                }
            },
            loaddata1(id) {
                let params = {
                    localPage: this.currentPage1, //加载默认参数
                    pageSize: this.pageSize1,
                    keywords: this.dname1,
                }
                //查询对应的明细
                this.$http.post("/dictDetail/" + id, params).then(res => {
                    console.log(res)
                    let {data, totals} = res.data; //解构
                    this.dictionaryDetailList = data;
                    this.total1 = totals;
                })
            },
            //双击
            double1(row) {
                this.drawer = true
                // console.log(row)
                this.parentId = row.id
                this.loaddata1(this.parentId)
            },
            //关闭子窗口
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            //重置。点击取消
            resetForm(addForm) {
                // this.$refs[addForm].resetFields();
                this.dialogFormVisible = false
            },
            //提交
            onSubmit(addForm) {
                //提交校验,校验成功
                        console.log(this.addForm)
                        this.$http.put("/dictType", this.addForm).then(res => {
                            //关闭dialog
                            this.dialogFormVisible = false
                            //加载数据
                            this.loadData()
                        })
            },
            //高级查询
            getUsers() {
                //跳转到第一页查询
                this.currentPage = 1
                this.loadData()
            },
            //增加
            handleAdd() {
                this.title = "添加";


                //清空表单
                for (let i in this.addForm) {
                    this.addForm[i] = ""
                }
                //显示修改框
                this.dialogFormVisible = true;
            },
            //修改
            handleEdit: function (index, row) {
                console.log(row);
                this.title = "修改";
                //弹出dialog
                this.dialogFormVisible = true;
                //回显数据(一般数据)
                for (let x in this.addForm) {
                    this.addForm[x] = row[x]
                }
            },
            //删除单条数据
            handleDel(index, row) {  //row行内所有数据
                this.$confirm('此操作将永久删除该数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.delete("/dictType/" + row.id).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //加载数据
            loadData() {
                let params = {
                    localPage: this.currentPage, //加载默认参数
                    pageSize: this.pageSize,
                    keywords: this.dname,

                }
                //请求地址  then请求成功  catch请求失败
                this.$http.post("/dictType", params).then(res => {
                    let {data, totals} = res.data; //解构
                    this.dictionaryTypeList = data;
                    this.total = totals;
                }).catch()
            },
            //批量删除
            batchRemove() {
                this.$confirm('此操作将永久删除这些数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/dictType/batchRemove", this.sels).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //点击分页,更改当前页
            handleCurrentChange(val) {
                this.currentPage = val;
                this.loadData()
            },
            //选择多少行的数据
            selsChange(rows) {
                this.sels = rows;
            },
            selsChange1(rows) {
                this.sels1 = rows;
            }
        },
        mounted() {
            //加载完毕之后执行
            this.loadData()
        }
    }
</script>

<style scoped>

</style>